#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif
#include <stdio.h>
#include <string.h>
#include "uartinfo.h"
#include "pinconfig.h"

#define IER_RBR		0x01
#define IER_THRE	0x02
#define IER_RLS		0x04

#define IIR_PEND	0x01
#define IIR_RLS		0x03
#define IIR_RDA		0x02
#define IIR_CTI		0x06
#define IIR_THRE	0x01

#define LSR_RDR		0x01
#define LSR_OE		0x02
#define LSR_PE		0x04
#define LSR_FE		0x08
#define LSR_BI		0x10
#define LSR_THRE	0x20
#define LSR_TEMT	0x40
#define LSR_RXFE	0x80

// ! a la vitesse maxi admissible du MAX232, aux alentours de 130000 baud en version standard
// 300:    DIVADDVAL=1, MULVAL=2,  DLM=13, DLL=144 si 100 MHz & PCLK=CCLK/4
// 1200:   DIVADDVAL=1, MULVAL=2,  DLM=3,  DLL=100 si 100 MHz & PCLK=CCLK/4
// 4800:   DIVADDVAL=1, MULVAL=2,  DLM=0,  DLL=217 si 100 MHz & PCLK=CCLK/4
// 9600:   DIVADDVAL=1, MULVAL=2,  DLM=0,  DLL=108 si 100 MHz & PCLK=CCLK/4
// 38400:  DIVADDVAL=1, MULVAL=2,  DLM=0,  DLL=27  si 100 MHZ & PCLK=CCLK/4
// 57600:  DIVADDVAL=1, MULVAL=2,  DLM=0,  DLL=18  si 100 MHZ & PCLK=CCLK/4
// 115200: DIVADDVAL=1, MULVAL=2,  DLM=0,  DLL=9   si 100 MHZ & PCLK=CCLK/4
// 230400: DIVADDVAL=9, MULVAL=13, DLM=0,  DLL=4   si 100 MHZ & PCLK=CCLK/4 ! NG (MAX232 limit ?!)

void UART1_Init_50(int baudrate)
{
	 // int lcr = 0;   // LPC_UARTx->LCR Line Control Register
	  int dlm = 0;   // LPC_UARTx->DLM Divisor Latch MSB
	  int dll = 54; // LPC_UARTx->DLL Divisor Latch LSB
	  int fdr = 0;   // LPC_UARTx->FDR Frational Baud Rate Divisor Register, contient divaddval et mulval
	    int divaddval = 1; // Baud rate prescaler divisor (bit 0->3 de FDR)
	    int mulval = 2;    // Baud rate prescaler multiplier (bit 4->7 de FDR)
	    fdr = (mulval<<4)|divaddval;

	  	  	  	LPC_SC->PCONP |= bit4; // Turn On UART1
	            LPC_UART1->LCR |= bit7;
	            //LPC_UART1->LCR |= lcr;
	            LPC_UART1->DLL = dll;
	            LPC_UART1->DLM = dlm;
	            LPC_UART1->FDR = fdr;
	            LPC_UART1->LCR = 0x03;	// 8 bits, no Parity, 1 Stop bit DLAB=0 -> Disable Access to Divisor Latch
	            LPC_UART1->FCR = 0x07;	// Enable and reset TX and RX FIFO
}

void UART1_Init_100(int baudrate)
{
	 // int lcr = 0;   // LPC_UARTx->LCR Line Control Register
	  int dlm = 0;   // LPC_UARTx->DLM Divisor Latch MSB
	  int dll = 108; // LPC_UARTx->DLL Divisor Latch LSB
	  int fdr = 0;   // LPC_UARTx->FDR Frational Baud Rate Divisor Register, contient divaddval et mulval
	    int divaddval = 1; // Baud rate prescaler divisor (bit 0->3 de FDR)
	    int mulval = 2;    // Baud rate prescaler multiplier (bit 4->7 de FDR)
	    fdr = (mulval<<4)|divaddval;

	  	  	  	LPC_SC->PCONP |= bit4; // Turn On UART1
	            LPC_UART1->LCR |= bit7;
	            //LPC_UART1->LCR |= lcr;
	            LPC_UART1->DLL = dll;
	            LPC_UART1->DLM = dlm;
	            LPC_UART1->FDR = fdr;
	            LPC_UART1->LCR = 0x03;	// 8 bits, no Parity, 1 Stop bit DLAB=0 -> Disable Access to Divisor Latch
	            LPC_UART1->FCR = 0x07;	// Enable and reset TX and RX FIFO
}

void UART1_Init_120(int baudrate)
{
	  // int lcr = 0;   // LPC_UARTx->LCR Line Control Register
	  int dlm = 0;   // LPC_UARTx->DLM Divisor Latch MSB
	  int dll = 130; // LPC_UARTx->DLL Divisor Latch LSB
	  int fdr = 0;   // LPC_UARTx->FDR Frational Baud Rate Divisor Register, contient divaddval et mulval
	    int divaddval = 1; // Baud rate prescaler divisor (bit 0->3 de FDR)
	    int mulval = 2;    // Baud rate prescaler multiplier (bit 4->7 de FDR)
	    fdr = (mulval<<4)|divaddval;

	  	  	  	LPC_SC->PCONP |= bit4; // Turn On UART1
	            LPC_UART1->LCR |= bit7;
	            //LPC_UART1->LCR |= lcr;
	            LPC_UART1->DLL = dll;
	            LPC_UART1->DLM = dlm;
	            LPC_UART1->FDR = fdr;
	            LPC_UART1->LCR = 0x03;	// 8 bits, no Parity, 1 Stop bit DLAB=0 -> Disable Access to Divisor Latch
	            LPC_UART1->FCR = 0x07;	// Enable and reset TX and RX FIFO
}


// Function to send character over UART
void UART1_Sendchar(char c)
{
	while( (LPC_UART1->LSR & LSR_THRE) == 0 );	// Block until tx empty
	LPC_UART1->THR = c;
	printf("\tenvoye[%c] - ASCII=%d\n",c,(int)c);
}
// ajoute le '\r'
void UART1_SendCmd2FB755(unsigned char *cmd)
{
 int i=0;
  while (cmd[i] != 0)
  {
   UART1_Sendchar(cmd[i]); // print each character
   i++;
  }
  UART1_Sendchar('\r');
}
// Function to get character from UART
char UART1_Getchar()
{
	char c;
	//printf("Waiting char....\n");
	while( (LPC_UART1->LSR & LSR_RDR) == 0 );  // Nothing received so just block
	c = LPC_UART1->RBR; // Read Receiver buffer register
	return c;
}

// Function to prints the string out over the UART
void UART1_PrintString(char *pcString)
{
	int i = 0;
	// loop through until reach string's zero terminator
	while (pcString[i] != 0)
	{
		UART1_Sendchar(pcString[i]); // print each character
		i++;
	}

}
